package org.jeecg.modules.demo.weixin.smtstoreduserpoint.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.weixin.entity.SmtStoredUserPoint;
import org.jeecg.modules.demo.weixin.smtstoreduserpoint.service.ISmtStoredUserPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 用户门店余额
 * @Author: jeecg-boot
 * @Date: 2023-11-17
 * @Version: V1.0
 */
@Api(tags = "用户门店余额")
@RestController
@RequestMapping("/weixin/smtstoreduserpoint/smtStoredUserPoint")
@Slf4j
public class SmtStoredUserPointController extends JeecgController<SmtStoredUserPoint, ISmtStoredUserPointService> {
    @Autowired
    private ISmtStoredUserPointService smtStoredUserPointService;

    /**
     * 分页列表查询
     *
     * @param smtStoredUserPoint
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户门店余额-分页列表查询")
    @ApiOperation(value = "用户门店余额-分页列表查询", notes = "用户门店余额-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SmtStoredUserPoint>> queryPageList(SmtStoredUserPoint smtStoredUserPoint,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<SmtStoredUserPoint> queryWrapper = QueryGenerator.initQueryWrapper(smtStoredUserPoint, req.getParameterMap());
        Page<SmtStoredUserPoint> page = new Page<SmtStoredUserPoint>(pageNo, pageSize);
        IPage<SmtStoredUserPoint> pageList = smtStoredUserPointService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param smtStoredUserPoint
     * @return
     */
//	@AutoLog(value = "用户门店余额-添加")
    @ApiOperation(value = "用户门店余额-添加", notes = "用户门店余额-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SmtStoredUserPoint smtStoredUserPoint) {
        smtStoredUserPointService.save(smtStoredUserPoint);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param smtStoredUserPoint
     * @return
     */
//	@AutoLog(value = "用户门店余额-编辑")
    @ApiOperation(value = "用户门店余额-编辑", notes = "用户门店余额-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SmtStoredUserPoint smtStoredUserPoint) {
        smtStoredUserPointService.updateById(smtStoredUserPoint);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//	@AutoLog(value = "用户门店余额-通过id删除")
    @ApiOperation(value = "用户门店余额-通过id删除", notes = "用户门店余额-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        smtStoredUserPointService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//	@AutoLog(value = "用户门店余额-批量删除")
    @ApiOperation(value = "用户门店余额-批量删除", notes = "用户门店余额-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.smtStoredUserPointService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户门店余额-通过id查询")
    @ApiOperation(value = "用户门店余额-通过id查询", notes = "用户门店余额-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SmtStoredUserPoint> queryById(@RequestParam(name = "id", required = true) String id) {
        SmtStoredUserPoint smtStoredUserPoint = smtStoredUserPointService.getById(id);
        if (smtStoredUserPoint == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(smtStoredUserPoint);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param smtStoredUserPoint
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmtStoredUserPoint smtStoredUserPoint) {
        return super.exportXls(request, smtStoredUserPoint, SmtStoredUserPoint.class, "用户门店余额");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmtStoredUserPoint.class);
    }


    /**
     * 查询用户点卡值
     *
     * @param uid
     * @param merId
     * @return
     */
    @ApiOperation(value = "查询用户点卡值", notes = "查询用户点卡值")
    @GetMapping(value = "/queryPoint")
    public BigDecimal queryPoint(@RequestParam Integer uid, @RequestParam String merId) {
        BigDecimal rtBig = new BigDecimal(-1);
        LambdaQueryWrapper<SmtStoredUserPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredUserPoint::getUserId, uid);
        queryWrapper.eq(SmtStoredUserPoint::getMerchantId, merId);
        List<SmtStoredUserPoint> serviceByIds = smtStoredUserPointService.list(queryWrapper);
        if (null == serviceByIds || serviceByIds.size() == 0) {
            SmtStoredUserPoint smtStoredUserPoint = new SmtStoredUserPoint();
            smtStoredUserPoint.setUserId(uid);
            smtStoredUserPoint.setMerchantId(merId);
            smtStoredUserPointService.save(smtStoredUserPoint);
            rtBig = BigDecimal.ZERO;
        }
        if (serviceByIds.size() == 1)
            rtBig = serviceByIds.get(0).getUserRest().add(serviceByIds.get(0).getValueGive());
        if (serviceByIds.size() > 1)
            rtBig = new BigDecimal(-9999);
        return rtBig;
    }

}
